<!DOCTYPE html>
          <head>
        <meta charset="utf-8">
            
            <title>
                「Android」Android Studio 1.x NDK环境搭建 | ttdevs
            </title>
            <meta content="width=device-width, initial-scale=1" name="viewport">
            <meta name="theme-color" content="#4184f3">
            
            
            <link href="/favicon.ico" rel="icon"/>
            

            <link rel="stylesheet" href="/css/highlight.light.css">
            <link rel="stylesheet" href="/css/prism-customize.css">
            <link rel="stylesheet" href="/css/nav-icon.css">
            <link rel="stylesheet" href="/css/waves.min.css">
            <link rel="stylesheet" href="/css/jquery.tocify.css">
            <link rel="stylesheet" href="/css/main.css">
            <link rel="stylesheet" href="/css/nav-indicator.css">
            
  

  
  <!-- 谷歌统计 -->
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-97465173-1', 'auto');
    ga('send', 'pageview');

  </script>
  
            </meta>
        </meta>
    </head>

    <body>
        <header>
            <!-- cover image or sth. -->
        </header>
        <div id="main" class="m-scene">
            
<div class="nav-wrapper">

    <div class="container">
        <nav>
            <div class="logo wave">
                <a href="/" id="logo">
                    ttdevs
                </a>
            </div>
            <div class="nav-toggle-icon" >
                <div class="material-hamburger">
                    <span>
                    </span>
                    <span>
                    </span>
                    <span>
                    </span>
                </div>
            </div>
            <div class="menu-wrapper">
                <div class="nav-indicator">
                </div>
                <ul class="menus">
                    
                     
                        <li>
                            <a class="wave " href="/">
                                首页
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/archives">
                                归档
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/about">
                                关于
                            </a>
                        </li>
                     
                    
                   
                </ul>
            </div>
        </nav>
    </div>
</div>
            <div class="container content">
                <div class="scene_element scene_element--fadein">
                    <div class="row">
    <div class="main">
        <article>
          
          <header class="post-header with-cover" style="background-image:url('/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/cover.jpg')" >
          
          </header>
          <h1 class="post-title">「Android」Android Studio 1.x NDK环境搭建</h1>

          <section class="post-info">
            <span class="post-date">1970/01/01</span>
            
            <span class="post-category">
                <a class="article-category-link" href="/categories/技术/">技术</a>
            </span>
            
            
            <span class="post-tags">
              <ul class="post-tag-list"><li class="post-tag-list-item"><a class="post-tag-list-link" href="/tags/Android/">Android</a></li></ul>
            </span>
            
          </section>

          <section class="post-content">
            <h2 id="下载NDK"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#下载NDK" class="headerlink" title="下载NDK"></a>下载NDK</h2><h3 id="下载"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#下载" class="headerlink" title="下载"></a>下载</h3><p><a href="http://developer.android.com/ndk/downloads/index.html" target="_blank" rel="external">NDK Download</a></p>
<p><a href="http://dl.google.com/android/ndk/android-ndk-r10e-linux-x86.bin" target="_blank" rel="external">mac: android-ndk-r10e-linux-x86.bin</a></p>
<h3 id="安装"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#安装" class="headerlink" title="安装"></a>安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">➜  ndk ls</div><div class="line">android-ndk-r10e-darwin-x86_64.bin</div><div class="line">➜  ndk chmod a+x android-ndk-r10e-darwin-x86_64.bin</div><div class="line">➜  ndk ./android-ndk-r10e-darwin-x86_64.bin</div></pre></td></tr></table></figure>
<h2 id="配置环境"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#配置环境" class="headerlink" title="配置环境"></a>配置环境</h2><h3 id="gradle-properties"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#gradle-properties" class="headerlink" title="gradle.properties"></a>gradle.properties</h3><p>在末尾添加一行： <code>android.useDeprecatedNdk=true</code></p>
<h3 id="添加build工具"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#添加build工具" class="headerlink" title="添加build工具"></a>添加build工具</h3><p>AS &gt; Preferences &gt; Tools &gt; External Tools</p>
<h4 id="add-javah"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#add-javah" class="headerlink" title="add javah"></a>add javah</h4><ul>
<li>Name: javah</li>
<li>Group: default(External Tools)</li>
<li>Description: generate C header</li>
<li>Options: select all</li>
<li>Show in: select all</li>
<li>Tools Settings:<ul>
<li>Program:  <code>$JDKPath$/bin/javah</code></li>
<li>Parameters:  <code>-v -d $ModuleFileDir$/src/main/jni -jni $FileClass$</code></li>
<li>Parameters:  <code>-bootclasspath $ModuleSdkPath$/platforms/android-23/android.jar -v -d $ModuleFileDir$/src/main/jni -jni $FileClass$</code> (if you need android object)</li>
<li>Working directory:  <code>$SourcepathEntry$</code></li>
</ul>
</li>
</ul>
<p>(PS: Right click on the class)</p>
<p><img src="https://raw.githubusercontent.com/ttdevs/Demo/master/ndkutil/image-javah.png" alt="项目结构"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">➜  ~ javah</div><div class="line">用法:</div><div class="line">  javah [options] &lt;classes&gt;</div><div class="line">其中, [options] 包括:</div><div class="line">  -o &lt;file&gt;                输出文件 (只能使用 -d 或 -o 之一)</div><div class="line">  -d &lt;dir&gt;                 输出目录</div><div class="line">  -v  -verbose             启用详细输出</div><div class="line">  -h  --help  -?           输出此消息</div><div class="line">  -version                 输出版本信息</div><div class="line">  -jni                     生成 JNI 样式的标头文件 (默认值)</div><div class="line">  -force                   始终写入输出文件</div><div class="line">  -classpath &lt;path&gt;        从中加载类的路径</div><div class="line">  -bootclasspath &lt;path&gt;    从中加载引导类的路径</div><div class="line">&lt;classes&gt; 是使用其全限定名称指定的</div><div class="line">(例如, java.lang.Object)。</div></pre></td></tr></table></figure>
<h4 id="add-ndkBuild"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#add-ndkBuild" class="headerlink" title="add ndkBuild"></a>add ndkBuild</h4><ul>
<li>Name: ndkBuild</li>
<li>Group: default(External Tools)</li>
<li>Description: ndk build</li>
<li>Options: select all</li>
<li>Show in: select all</li>
<li>Tools Settings:<ul>
<li>Program:  <code>/Users/ttdevs/android/android-ndk-r10e/ndk-build</code></li>
<li>Parameters:  <code>NDK_LIBS_OUT=$ModuleFileDir$/libs</code></li>
<li>Working directory:  <code>$ModuleFileDir$/src/main</code></li>
</ul>
</li>
</ul>
<p>(PS: Right click on the module name)</p>
<p><img src="https://raw.githubusercontent.com/ttdevs/Demo/master/ndkutil/image-ndkbuild.png" alt="项目结构"></p>
<h2 id="编写代码"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#编写代码" class="headerlink" title="编写代码"></a>编写代码</h2><h3 id="新建一个Module：ndkutil"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#新建一个Module：ndkutil" class="headerlink" title="新建一个Module：ndkutil"></a>新建一个Module：ndkutil</h3><h3 id="修改ndkutil的build-gradle"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#修改ndkutil的build-gradle" class="headerlink" title="修改ndkutil的build.gradle"></a>修改ndkutil的build.gradle</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">android &#123;</div><div class="line">    ...</div><div class="line">    defaultConfig &#123;</div><div class="line">        ...</div><div class="line">        ndk &#123;</div><div class="line">            moduleName <span class="string">"ndkutil"</span></div><div class="line">//            abiFilters <span class="string">"armeabi-v7a"</span>, <span class="string">"x86"</span></div><div class="line">//            stl <span class="string">"gnustl_static"</span></div><div class="line">//            cFlags <span class="string">"-std=c++11 -Wall"</span></div><div class="line">//            ldLibs <span class="string">"log"</span>, <span class="string">"jnigraphics"</span>, <span class="string">"EGL"</span>, <span class="string">"GLESv3"</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    sourceSets.main &#123;</div><div class="line">        jni.srcDirs = []</div><div class="line">        jniLibs.srcDir <span class="string">"libs"</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="创建工具类CipherUtil"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#创建工具类CipherUtil" class="headerlink" title="创建工具类CipherUtil"></a>创建工具类CipherUtil</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> com.ttdevs.ndk;</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CipherUtil</span> </span>&#123;</div><div class="line">    <span class="keyword">static</span> &#123;</div><div class="line">        System.loadLibrary(<span class="string">"Cipher"</span>);</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> String <span class="title">getCipherKey</span><span class="params">()</span></span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="生成头文件com-ttdevs-ndk-CipherUtil-h"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#生成头文件com-ttdevs-ndk-CipherUtil-h" class="headerlink" title="生成头文件com_ttdevs_ndk_CipherUtil.h"></a>生成头文件com_ttdevs_ndk_CipherUtil.h</h3><p>在刚才创建的CipherUtil.java上点击右键 &gt; External Tools &gt; javah<br>此时会在main目录下生成: jni/com_ttdevs_ndk_CipherUtil.h</p>
<h3 id="编写代码-1"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#编写代码-1" class="headerlink" title="编写代码"></a>编写代码</h3><p>都是在ndkutil/src/main/jni目录下</p>
<ul>
<li>创建C源码:Cipher.cpp</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"com_ttdevs_ndk_CipherUtil.h"</span></span></div><div class="line"></div><div class="line"><span class="function">JNIEXPORT jstring JNICALL <span class="title">Java_com_ttdevs_ndk_CipherUtil_getCipherKey</span><span class="params">(JNIEnv *env, jclass)</span></span>&#123;</div><div class="line">    <span class="keyword">return</span> (*env).NewStringUTF(<span class="string">"Hello World!"</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>创建Android.mk</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">LOCAL_PATH := $(call my-dir)</div><div class="line"></div><div class="line">include $(CLEAR_VARS)</div><div class="line"></div><div class="line">LOCAL_MODULE := Cipher</div><div class="line">LOCAL_SRC_FILES := Cipher.cpp</div><div class="line"></div><div class="line">include $(BUILD_SHARED_LIBRARY)</div></pre></td></tr></table></figure>
<ul>
<li>创建Application.mk</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">APP_MODULE := Cipher</div><div class="line">APP_ABI := all</div></pre></td></tr></table></figure>
<h3 id="编译生成so文件"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#编译生成so文件" class="headerlink" title="编译生成so文件"></a>编译生成so文件</h3><p>在module（ndkutil）上点击右键 &gt; External Tools &gt; ndkBuild ，可以看到如下log：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">/Users/ttdevs/android/android-ndk-r10d/ndk-build NDK_LIBS_OUT=/Users/ttdevs/android/workspace/Demo/ndkutil/libs</div><div class="line">[arm64-v8a] Compile++      : Cipher &lt;= Cipher.cpp</div><div class="line">[arm64-v8a] SharedLibrary  : libCipher.so</div><div class="line">[arm64-v8a] Install        : libCipher.so =&gt; /Users/ttdevs/android/workspace/Demo/ndkutil/libs/arm64-v8a/libCipher.so</div><div class="line">[x86_64] Compile++      : Cipher &lt;= Cipher.cpp</div><div class="line">[x86_64] SharedLibrary  : libCipher.so</div><div class="line">[x86_64] Install        : libCipher.so =&gt; /Users/ttdevs/android/workspace/Demo/ndkutil/libs/x86_64/libCipher.so</div><div class="line">[mips64] Compile++      : Cipher &lt;= Cipher.cpp</div><div class="line">[mips64] SharedLibrary  : libCipher.so</div><div class="line">[mips64] Install        : libCipher.so =&gt; /Users/ttdevs/android/workspace/Demo/ndkutil/libs/mips64/libCipher.so</div><div class="line">[armeabi-v7a] Compile++ thumb: Cipher &lt;= Cipher.cpp</div><div class="line">[armeabi-v7a] SharedLibrary  : libCipher.so</div><div class="line">[armeabi-v7a] Install        : libCipher.so =&gt; /Users/ttdevs/android/workspace/Demo/ndkutil/libs/armeabi-v7a/libCipher.so</div><div class="line">[armeabi] Compile++ thumb: Cipher &lt;= Cipher.cpp</div><div class="line">[armeabi] SharedLibrary  : libCipher.so</div><div class="line">[armeabi] Install        : libCipher.so =&gt; /Users/ttdevs/android/workspace/Demo/ndkutil/libs/armeabi/libCipher.so</div><div class="line">[x86] Compile++      : Cipher &lt;= Cipher.cpp</div><div class="line">[x86] SharedLibrary  : libCipher.so</div><div class="line">[x86] Install        : libCipher.so =&gt; /Users/ttdevs/android/workspace/Demo/ndkutil/libs/x86/libCipher.so</div><div class="line">[mips] Compile++      : Cipher &lt;= Cipher.cpp</div><div class="line">[mips] SharedLibrary  : libCipher.so</div><div class="line">[mips] Install        : libCipher.so =&gt; /Users/ttdevs/android/workspace/Demo/ndkutil/libs/mips/libCipher.so</div><div class="line"></div><div class="line">Process finished with exit code 0</div></pre></td></tr></table></figure>
<p>此时我们可以发现在ndkutil/libs/目录下生成的so文件。</p>
<h3 id="测试代码"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#测试代码" class="headerlink" title="测试代码"></a>测试代码</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">String cipher = CipherUtil.getCipherKey();</div></pre></td></tr></table></figure>
<h2 id="项目结构"><a href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/#项目结构" class="headerlink" title="项目结构"></a>项目结构</h2><p><img src="https://raw.githubusercontent.com/ttdevs/Demo/master/ndkutil/image-dir.png" alt="项目结构"></p>

          </section>
        </article>
        

       
        <div class="pager">
          
            <a class="post-prev pager-item" href="/1970/01/01/[Android]Android闹钟遇到的那些坑/" >
              <strong class="article-nav-caption">上一篇</strong>
              <p class="post-nav-title">「Android」Android闹钟遇到的那些坑</p>
            </a>
          
          
            <a class="post-next pager-item" href="/1970/01/01/[Android]DataBinding给Button设置背景无效/">
              <strong class="article-nav-caption">下一篇</strong>
              <p class="post-nav-title">「Android」DataBinding给Button设置背景无效</p>
            </a>
          
        </div>
        

         <!-- comments -->
        <div class="comment-section">
  
    


</div>

    </div>
    
    <aside>
        <div id="toc">
        </div>
    </aside>
    
</div>

                </div>
            </div>
        </div>
        <footer class="footer">
    <p>由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动，搭载<a href="https://github.com/wayou/hexo-theme-gstyle">gstyle</a>主题</p>
    <p>
        &copy; 2017 ttdevs
    </p>
</footer>
<script src="/lib/jquery.js"></script>
<script src="/lib/waves.js"></script>
<script src="/lib/jquery-ui.js"></script>
<script src="/lib/jquery.tocify.js"></script>
<script src="/js/main.js"></script>

    </body>
</html>
